
import socket
import sys
import threading



def receive_thread_proc(sock):
    while True:
        try:
            msg = sock.recv(500).decode('gb2312')
            if( -1 != msg.find("Jke:/$")):
                #idx=msg.find(' ')
                print(msg[7:],end='')
            else:
                print(msg ,end='')

        except socket.error as err:
            print(err)
            continue
            

# #############################################
#           正式执行语句从这里开始
# #############################################
n = len(sys.argv)
if(2!=n):
    print('参数个数不对')
    print('Usage: python '+sys.argv[0]+' <proxy server addr>')
    sys.exit(2)

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((sys.argv[1], 10086))
except socket.error as msg:
    print(msg)
    print(sys.exit(1))

r_thead = threading.Thread(target=receive_thread_proc,args=(s,) ,daemon=True)
r_thead.start()

while True:
    msg = input("(proxy: " + sys.argv[1] + ")\r\nJke:/$ ")
    s.send(msg.encode('utf8'))

